{# empty Twig template #}
<script type="text/javascript" class="source below">
// Note method 2) and 3) use `one`, this is because if `refresh` is called those events are triggered
$(function () {
	$("#demo2")
		.jstree({ "plugins" : ["themes","html_data","ui"] 
        })
		// 1) the loaded event fires as soon as data is parsed and inserted
		.bind("loaded.jstree", function (event, data) {})
		// 2) but if you are using the cookie plugin or the core `initially_open` option:
		.one("reopen.jstree", function (event, data) { })
		// 3) but if you are using the cookie plugin or the UI `initially_select` option:
		.one("reselect.jstree", function (event, data) { })
                .bind("select_node.jstree", function (event, data) { 
                    //console.log(":)"); 
                    var url=data.rslt.obj.attr("id"); 
                    //console.log(data);
                    $.ajax({type: 'POST', url:url, script:true, cache:true,
                    beforeSend: function(response){ $('#loading').append('<div class="loader"><img src="{{asset('bundles/news/images/loaders/loader.gif')}}" alt=""></div>');},
                    error: function(){ $('#formcontent').html('<div class="nNote nFailure hideit"><p><strong>FAILURE: </strong>Oops sorry. That action is not valid, or our servers have gone bonkers</p></div>');},
                    success: function(response) {
			if (response === "false"){
                                console.log(response);
			}else{
		    
                        $('#formcontent').html(response);
                        
			}
                } });
            
                });

});


</script>
 <div class="left" id="form1" > 
          <!-- Form -->
<form action="{{ path('em_geography') }}" method="post" id="valid" class="mainForm">
<fieldset>
    <div class="widget">    
	 <div class="head" id="loading"><h5 class="iHome">Create category</h5></div>
         {% if status_form==1 %}
	 <div class="nNote nSuccess hideit">
                <p><strong>SUCCESS: </strong> Category created.</p>
            </div>
	
	{% elseif status_form==2 %}
	<div class="nNote nFailure hideit">
		<p><strong>FAILURE: </strong>Oops sorry. Something was wrong, try again.</p>
	</div>
         {% elseif status_form==3 %}
	<div class="nNote nFailure hideit">
		<p><strong>FAILURE: </strong>You cannot add more than 4 levels .</p>
	</div>
          {% elseif status_form==17 %}
	<div class="nNote nFailure hideit">
		<p><strong>FAILURE: </strong>You cannot delete this item because has more levels.</p>
	</div>
       
	 {% endif %}
	 
<div class="rowElem">
<label>Geo Type Level:<span class="req">*</span></label>
<div class="formRight">{{ form_widget(form.level) }}</div>
<div class="formRight">{{ form_errors(form.level) }}</div>
<div class="fix"></div>
</div> 
<div class="rowElem">
<label>Name<span class="req">*</span></label>
<div class="formRight">{{ form_widget(form.name) }}</div>
<div class="formRight">{{ form_errors(form.name) }}</div>
<div class="fix"></div>
</div> 
<div id="combo">
</div>
<div id="combo2">
</div>
    <input type="submit" value="New Category" class="greyishBtn submitForm" />
    <div class="fix"></div>
	</div>
</fieldset>
</form>
</div>
<!--Derecho-->
<div class="right"> 
<div class="widget">
    <div class="head" id="opened"><h5>Geography directory</h5></div>
    <div id="demo2" class="demo" >

                         
                               {% for key, geo in geography%}
                                {% if geo.level.id==1 %}
                                    <ul><li id="{{ path('em_geography_find', {'id': geo.id}) }}" class="jstree-open">
                                      <a href="#" id="cat" >{{ geo.name}}</a>
                                      {% for geo1 in geography%}
                                            {% if geo1.geoParent!=0%}
                                                {% if geo.id==geo1.geoParent%}
                                                  <ul><li id="{{ path('em_geography_find', {'id': geo1.id}) }}" class="jstree-open">
                                                  <a> {{geo1.name}}</a>
                                                  {% for sgeo in geography%}
                                                    {% if geo1.id==sgeo.geoParent%}
                                                            <ul><li id="{{ path('em_geography_find', {'id': sgeo.id}) }}" class="jstree-open">
                                                            <a> {{sgeo.name}}</a>
                                                            {% for subgeo in geography%}
                                                                {% if sgeo.id==subgeo.geoParent%}
                                                                    <ul><li id="{{ path('em_geography_find', {'id': subgeo.id}) }}" class="jstree-open">
                                                                    <a> {{subgeo.name}}</a>
                                                                    </li></ul>  
                                                                {% endif %}
                                                            {% endfor %}
                                                             </li></ul>  
                                                    {% endif %}
                                                  {% endfor %}
                                                  </li></ul>          
                                                 {% endif%} 
                                            {% endif%}
                                      {% endfor%}
                                      </li></ul>
                                {% endif %}
                            {% endfor %}
    </div>
    </div>
  </div>
<script>
$('#category_form_level').on('change', function() {
 
 var data=parseInt(this.value)-1;
 var action='{{ path('em_geography_levels',{'level':'data'}) }}';   
 action=action.replace("data",data);
 console.log(action); 
 if(data!==-1)
 {
 $.ajax({type: 'POST', url:action, script:true, cache:false,data: data,
                    beforeSend: function(response){ 
                            $('#combo').html('<div align="center"><img src="{{asset('bundles/news/images/loaders/loader.gif')}}" alt="" class="p12"><br>Loading data...</div>');
                            $('#combo2').html("   ");
                    },
                    error: function(response){console.log(response);},
                    success: function(response) {
			if (response === "false"){

			}else{
                        $('#combo').html(response);
                        $('#combo2').html("   ");
                        
			}
                } });
 }
 else
 {
 $('#combo').html("");
 }
});



$( "#valid" ).submit(function( event ) {
if($("#geoparent1").val()==="N")
    {
      //  console.log("Error!! ");
  //   $('#combo').append('<span class="req">*</span>Invalid action.');
  //   $(this).find('input[type=submit]').attr('disabled', 'disabled');
  alert('Invalid action');
        return false;
    }
if($("#geoparent").val()==="N")
    {
      //  console.log("Error!! ");
  //   $('#combo2').append('<span class="req">*</span>Invalid action.');
   //  $(this).find('input[type=submit]').attr('disabled', 'disabled');
   alert('Invalid action');
        return false;
    }
    else
    {
    var action=$('#valid').attr('action');
    var datos = $(this).serialize();     
    console.log(datos);
    $.ajax({type: 'POST', url:action, script:true, cache:false,data: datos,
                      // beforeSend: function(response){ $('#formcontent').html('<div align="center"><img src="{{asset('bundles/news/images/loaders/loader8.gif')}}" alt="" class="p12"><br>Loading data...</div>');},
                       error: function(response){console.log(response);},
                       success: function(response) {
                           if (response === "false"){

                           }else{
                          // console.log(response);
                           $('#formcontent').html(response);

                           }
                   } });
           return false;   
    }

 
 
});
$("#delete_btn").click(function(event){

var url=$('#catid').val();
jConfirm('Do you want delete this category?', 'Delete category', function(response) {
			//jAlert('Confirmed: ' + response, 'Confirmation Results');
                        if(response){
                        $.ajax({type: 'POST', url:url, script:true, cache:false,
                        // beforeSend: function(response){ $('#formcontent').html('<div align="center"><img src="{{asset('bundles/news/images/loaders/loader8.gif')}}" alt="" class="p12"><br>Loading data...</div>');},
                         error: function(){ $('#formcontent').html('<div class="nNote nFailure hideit"><p><strong>FAILURE: </strong>Oops sorry. That action is not valid, or our servers have gone bonkers</p></div>');},
                         success: function(response) {
                                 if (response === "false"){

                                 }else
                                 {
                                
                                    $('#formcontent').html(response);

                                 }
                         } });
                        }
                      return false;
                      
		});
/*$.ajax({type: 'POST', url:url, script:true, cache:true,
                   // beforeSend: function(response){ $('#formcontent').html('<div align="center"><img src="{{asset('bundles/news/images/loaders/loader8.gif')}}" alt="" class="p12"><br>Loading data...</div>');},
                    error: function(){ $('#formcontent').html('<div class="nNote nFailure hideit"><p><strong>FAILURE: </strong>Oops sorry. That action is not valid, or our servers have gone bonkers</p></div>');},
                    success: function(response) {
			if (response === "false"){

			}else{
		    
                        $('#formcontent').html(response);
                        
			}
                } });*/
        
});
</script>   {# empty Twig template #}
